const WebSocket = require('ws');
const { wsf, wsfb } = require('./websocket_server.js')
const { findAvailablePort } = require('./AvailablePort.js')

const conf = {
    port: 59420,
    servers: {}
}

function websocketServer(server_name, callBack) {
    findAvailablePort(conf.port, port => {
        conf.port = port
        console.log(
            '\x1B[44m\x1b[37m\x1b[44m %s \x1b[0m%s',
            server_name.toUpperCase(),
            ` 端口 \x1B[36m${port}\x1B[0m 可用`
        )

        const wss = new WebSocket.Server({ port: port })

        conf.servers[server_name] = wss

        wsf[server_name] && wsf[server_name](wss)
        callBack && callBack(wss)
    })
}


websocketServer('test')
// websocketServer('test', wss => wsfb(wss, (msg, send, error) => {
//     const header = {
//         response: msg?.type,
//         token: msg?.token,
//         time: Date.now(),
//     }
//     sendmsg = (data, code = 0) => {
//         send({ code, data, header })
//     }

//     switch (msg.type) {
//         case 'check':
//             sendmsg('ok')
//             break;

//         default:
//             sendmsg('握手失败')
//             break;
//     }
// }))